package com.zhangdi.demo.jdbc.tree;

import com.fasterxml.jackson.annotation.JsonIgnore;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

public class Node<T> {
    public Node() {
        this.children = new ArrayList<>();
        this.allChildren = new ArrayList<>();
    }

    /**
     * 当前节点
     */
    private long id;
    private String name;
    private long pid;

    private List<T> children;

    @JsonIgnore
    public boolean child = false;
    @JsonIgnore
    private List<T> allChildren;

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public long getPid() {
        return pid;
    }

    public void setPid(long pid) {
        this.pid = pid;
    }

    public boolean getChild() {
        return child;
    }

    public void setChild(boolean child) {
        this.child = child;
    }

    public void setAllChildren(List<T> allChildren) {
        this.allChildren = allChildren;
    }

    public List<T> getChildren() {
        return children;
    }

    public void setChildren(List<T> children) {
        this.children = children;
    }

    public void add(T nodeBaseInfo) {
        this.children.add(nodeBaseInfo);
    }

    public List<T> getAllChildren() {
        if (this.allChildren.isEmpty()) {
            for (T treeNode : this.children) {
                this.allChildren.add(treeNode);
                try {
                    this.allChildren.addAll((ArrayList<T>) treeNode.getClass().getMethod("ALL_CHILDREN_METHOD_NAME").invoke(treeNode));
                } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
                    e.printStackTrace();
                }
            }
        }
        return this.allChildren;
    }
}
